package xzy.Controller;

import lombok.SneakyThrows;
import net.sf.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xzy.dao.GradeMapper;
import xzy.pojo.Grade;
import xzy.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "Grade_save_Servlet")
public class Grade_save_Servlet extends HttpServlet {
    Grade grade=new Grade();
    private static ApplicationContext context=new ClassPathXmlApplicationContext("application.xml");
    GradeMapper gradeMapper= (GradeMapper) context.getBean("gradeMapper");
    @SneakyThrows
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("保存页面");
        request.setCharacterEncoding("utf-8");
        String gradeName=request.getParameter("gradeName");
        String gradeDesc=request.getParameter("gradeDesc");
        grade=new Grade(gradeName,gradeDesc);
        int result;
        try{
            int saveNums=0;
            saveNums=gradeMapper.gradeAdd(grade);
            if(saveNums>0){
                result = 1;
            }else{
                result = 0;
            }
        }catch (Exception e){
            result = 0;
        }
        ResponseUtil.write(response, result);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
